<?php
namespace app\admin\controller;
use app\common\controller\ApiCore;

class Api extends ApiCore {
	/**
	 * @api
	 * @name 模块和插件列表
	 * @desc 权限等同于 'admin/Index/index'
	 * <method get/post{
	 * @params{name:verify_code,type:string,must:true,title:密钥,desc:如果未传递或者过期,返回错误}
	 * @return{type:string,desc:所有的模块和插件数据}
	 * }method>
	 */
    public function extend_lists(){
		//获取模块名称和ID
		$ModuleList = db('Module')->field('id,title,name')->select();
		$Temp	= [];
		foreach($ModuleList as $K => $V){
			$Temp[] = [
				'value' => $V['id'],
				'text' => '模块 '.$V['title'].' ['.$V['name'].']',
			];
		}
		$Return['module'] = $Temp;
		//获取模块名称和ID
		$PlugList = db('Plug')->field('id,title,name')->select();
		$Temp	= [];
		foreach($PlugList as $K => $V){
			$Temp[] = [
				'value' => $V['id'],
				'text' => '插件 '.$V['title'].' ['.$V['name'].']',
			];
		}
		$Return['plug'] = $Temp;
		$Return['extend'] = array_merge($Return['module'],$Return['plug']);
		return 'var _ExtendList='.json_encode($Return);
    }
	/**
	 * @api
	 * @name 字典列表
	 * @desc 权限等同于 'admin/Index/index'
	 * <method get/post{
	 * @params{name:verify_code,type:string,must:true,title:密钥,desc:如果未传递或者过期,返回错误}
	 * @return{type:string,desc:所有的字典数据}
	 * }method>
	 */
    public function dictionary_lists(){
		$Data = model('app\common\logic\Dictionary')->_Cache(true);
		return 'var _Dictionary='.json_encode($Data);
    }
	/**
	 * @api
	 * @name 插件方法列表
	 * @desc 权限等同于 'admin/Action/index'
	 * <method get/post{
	 * @params{name:verify_code,type:string,must:true,title:密钥,desc:如果未传递或者过期,返回错误}
	 * @params{name:plug_id,type:string,must:true,title:插件ID,desc:如果未传递或者过期,返回空Json}
	 * @return{type:json,desc:排除系统方法外的所有插件内置方法}
	 * }method>
	 */
    public function plug_func_list(){
		$PlugID = input('plug_id','');
		if($PlugID == ''){return json([]);}
		//获取模块名称和ID
        $_PlugName = db('Plug')->where(['id'=>$PlugID])->value('name');
		$_PlugClass = get_plug_class($_PlugName);
		$_Plug = new $_PlugClass();
		$_PlugFunc = get_class_methods($_Plug);
		$_PlugFunc = array_diff($_PlugFunc,['__construct','run','getName','fetch','display','show','assign','getError']);
		foreach($_PlugFunc as $K => $V){
			$_List[] = [
				'value' => $V,
				'text' => $V,
			];
		}
		return json($_List);
    }
    
	/**
	 * @api
	 * @name Icon图标列表
	 * @desc 权限等同于 'admin/AuthRule/index'
	 * <method get/post{
	 * @params{name:verify_code,type:string,must:true,title:密钥,desc:如果未传递或者过期,返回错误}
	 * @return{type:json,desc:读取配置中的 icon_paths 路径对应的图标文件}
	 * }method>
	 */
    public function icon_lists(){
		$icon_paths = config('icon_paths');
		$_Temp = [];
		foreach($icon_paths as $K => $V){
			$Str = file_get_contents($V);//将整个文件内容读入到一个字符串中
			preg_match("/font-family: \"(.*?)\";/i",$Str,$Mat1);
			$Pre = $Mat1[1];
			preg_match_all("/.(.*?):before/i",$Str,$Mat2);
			foreach($Mat2[1] as $K2 => $V2){
				$_Temp[] = [
					'iconCls'=>$Pre.' '.$V2
				];
			}
		}
		return json($_Temp);
    }
	
	/**
	 * @api
	 * @name 角色列表
	 * @desc 权限等同于 'admin/Index/index'
	 * <method get/post{
	 * @params{name:rtype,type:integer,must:false,title:返回类型,desc:0为含根节点,其他为不含}
	 * @return{type:json,desc:含根节点}
	 * @return{type:json,desc:不含根节点}
	 * }method>
	 */
	public function group_lists() {
		$_Data = db( 'AuthGroup' )->field( 'id,p_id,title as text' )->order( 'id asc' )->select();
		$T = input('rtype',0);
		if($T<1){
			$_Data[] = [ 'id' => 0, 'p_id' => -1, 'text' => '根节点', 'leaf' => false, 'children' => [] ];
			$_List = _Ext_ListToTree ( $_Data, 'id', 'p_id', 'children', -1 ,true);
		}else{
			$_List = _Ext_ListToTree ( $_Data, 'id', 'p_id', 'children', 0 ,true);
		}
		return json( $_List );
	}
	public function rule_lists() {
		$_Data = db( 'AuthRule' )->field( 'id,p_id,icon as iconCls,title as text' )->order( 'id asc' )->select();
		$T = input('rtype',0);
		if($T<1){
			$_Data[] = [ 'id' => 0, 'p_id' => -1, 'text' => '根节点', 'leaf' => false, 'children' => [] ];
			$_List = _Ext_ListToTree ( $_Data, 'id', 'p_id', 'children', -1 ,true);
		}else{
			$_List = _Ext_ListToTree ( $_Data, 'id', 'p_id', 'children', 0 ,true);
		}
		return json( $_List );
	}

}